// pages/biz/my/myCustom/myCustom.js

//引入请求接口文件
import Serv from './myCustomServ';
var regeneratorRuntime = require("../../../../lib/js/runtime-module.js")

Page({
  data: {
    choice:false,
    cIndex:0,
    // choiceAttr:[
    //   { name:'全部推荐'},
    //   { name:'近3日推荐'},
    //   { name:'近一周推荐'},
    //   // { name:'有效期少于3日'},-二期迭代
    //   // { name:'有效期少于一周'},
    // ],
    //筛选的客户列表
    customList: [],
    //名字或者电话号码
    nameOrMobile: '',
    housesList:[],
    payload:{}
  },
  clearInput(){
    this.setData({
      nameOrMobile:''
    })
  },
  makeCall:function(e){
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber:phone,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // choiceIndex:function(e){
  //   let index = e.currentTarget.dataset.index
  //   this.setData({ cIndex:index})
  //   this.handleSearch("time", index)
  // },
  // showChoice:function(e){
  //   console.log(e)
  //   let {choice} = this.data
  //   this.setData({
  //     choice:!choice,
  //   })
  // },
  //跳转到详情页
  router:function(e){
    // console.log(e)
    let clueId = e.currentTarget.dataset.clueid
    wx.navigateTo({
      url: `../customDetail/customDetail?id=${clueId}`,
    })    
  },
  //跳转到推荐页
  routerShare:function(e){
    console.log(e,"share")
    let id = e.currentTarget.dataset.productid,
    clueId = e.currentTarget.dataset.clueid
    wx.navigateTo({
      url: `../../recommend/recommend?id=${id}&clueId=${clueId}`,
    })    
  },
  hideChoice:function(){
    this.setData({
      choice: false,
    })
  },
  //筛选客户列表
  async getCustomList(params = {}) {
    // params.accountId = "232"
    let { rows, pageNo, pageSize, total } = await Serv.customGet(params)
    let { customList,housesList }  = this.data
    rows.map(item=>{
      let {productId,productName,cityName} = item
      let productInfo={}
      let status = housesList.some(i=>i.productId == item.productId)
      if(!status){
        productInfo.productName=productName
        productInfo.productId=productId,
        productInfo.cityName = cityName
        productInfo.select = false
        housesList.push(productInfo)
      }
    })
      console.log('housesList-------------------',housesList)
    if(pageNo>1){
      customList = customList.concat(rows)
      this.setData({
        customList
      })
    }else{
      this.setData({
        customList: rows ? rows : []
      })
    }
    this.setData({
      pageNo, pageSize, total,housesList
    })
    
  },
  //操作数据
  inputChange(e){
    this.setData({nameOrMobile: e.detail.value})
  },
  //搜索
  handleSearch(type, choiceIdx){
    let { nameOrMobile } = this.data;
    let params = {}
    if(type == 'time') {
      //参数-日期格式化
      let recommendTimeMap = {1: -3, 2: -7}
      choiceIdx ? this.getCustomList({recommendTime: recommendTimeMap[choiceIdx]}) : this.getCustomList()

    } else {
       this.getCustomList({nameOrMobile})
    }
  },
  selectChange(option){
    console.log(option.detail)
    let payload = {}
    payload = option.detail
    payload.pageNo = 1
    payload.pageSize = 10
    this.getCustomList(payload)
    this.setData({
      payload
    })
  },
  houseChange(options){
    // console.log(options.detail)
    this.setData({
      housesList:options.detail
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   //获取客户列表
   this.getCustomList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.setData({ nameOrMobile: '', cIndex:0 })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { customList, total, pageNo, pageSize, payload } = this.data
    if (customList.length < total) {
       pageNo=pageNo+1
      payload = { ...payload, pageNo, pageSize}
       console.log(pageNo)
      this.getCustomList({ ...payload })
      this.setData({ payload})
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})